package com.sam.demo.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;

import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisTestConfiguration {

    @Bean(name = "stringRedisTestTemplate")
    public StringRedisTemplate stringRedisTemplate(@Value("${spring.redis.redis-test.host}") String hostName,
            @Value("${spring.redis.redis-test.port}") int port, @Value("${spring.redis.redis-test.password}") String password,
            @Value("${spring.redis.redis-test.pool.max-idle}") int maxIdle, @Value("${spring.redis.redis-test.pool.max-active}") int maxTotal,
            @Value("${spring.redis.redis-test.database}") int index, @Value("${spring.redis.redis-test.pool.max-wait}") long maxWaitMillis) {
        StringRedisTemplate temple = new StringRedisTemplate();
        temple.setConnectionFactory(
                connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis, false));
        return temple;
    }
    
    @SuppressWarnings("rawtypes")
	@Bean(name = "redisTestTemplate")
    public RedisTemplate redisTemplate(@Value("${spring.redis.redis-test.host}") String hostName,
            @Value("${spring.redis.redis-test.port}") int port, @Value("${spring.redis.redis-test.password}") String password,
            @Value("${spring.redis.redis-test.pool.max-idle}") int maxIdle, @Value("${spring.redis.redis-test.pool.max-active}") int maxTotal,
            @Value("${spring.redis.redis-test.database}") int index, @Value("${spring.redis.redis-test.pool.max-wait}") long maxWaitMillis) {
    	RedisTemplate temple = new RedisTemplate();
        temple.setConnectionFactory(
                connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis, false));
        return temple;
    }

    public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle,
            int maxTotal, int index, long maxWaitMillis, boolean testOnBorrow) {
        JedisConnectionFactory jedis = new JedisConnectionFactory();
        jedis.setHostName(hostName);
        jedis.setPort(port);
        if (!StringUtils.isEmpty(password)) {
            jedis.setPassword(password);
        }
        if (index != 0) {
            jedis.setDatabase(index);
        }
        jedis.setPoolConfig(poolCofig(maxIdle, maxTotal, maxWaitMillis, testOnBorrow));
        // 初始化连接pool
        jedis.afterPropertiesSet();
        RedisConnectionFactory factory = jedis;

        return factory;
    }

    public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
        JedisPoolConfig poolCofig = new JedisPoolConfig();
        poolCofig.setMaxIdle(maxIdle);
        poolCofig.setMaxTotal(maxTotal);
        poolCofig.setMaxWaitMillis(maxWaitMillis);
        poolCofig.setTestOnBorrow(testOnBorrow);
        return poolCofig;
    }
}
